var SmallBookList = {
    init:function(_path, _inner){
        this.page = -1;
        this.total = 0;
        this.count = 0;
        this.first = 0;
        this.path = _path;
        this.inner = _inner;
        this.changePage();
    },
    changePage:function(){
        this.getData(++this.page);
    },
    createSmallBook:function(index, small){
        var smallBook = $("<div></div>").addClass("smallBook").attr("bookid", small.id).attr("isbn13", small.isbn13).attr("data-toggle","tooltip")
            .attr("data-placement", "left").attr("title",small.title+"\n"+small.subtitle).attr("style","width:282px");
        smallBook.append($("<div></div>").text(index+1));
        smallBook.append($("<img src=''/>").attr("src", small.smallImage).attr("onerror", "this.src='${path}/images/default-book.png'"));
        var innerB = $("<div></div>");
        innerB.append($("<div></div>").text(small.title+" "+small.subtitle));
        innerB.append($("<div></div>").text(small.author === null || small.author === '' ? "无 " : small.author));
        innerB.append($("<div></div>").text(small.isbn13 === null || small.isbn13 === '' ? '无' : small.isbn13));
        smallBook.append(innerB);
        this.addClickListener(smallBook);
        return smallBook;
    },
    addClickListener:function(bookBox){
        bookBox.click(function(){
            window.open(SmallBookList.path+"/page/book/id/"+$(this).attr("bookid")+".do");
        });
    },
    insertIntoInner:function(data){
        if(data !== null){
            this.inner.html('');
            for(var i = 0; i < data.length; i++){
                var smallBook = data[i];
                var box = this.createSmallBook(i, smallBook);
                this.inner.append(box);
                console.log("添加一个");
            }
        }else{
            console.log("切换页面失败");
        }
    },
    getData:function(page){
        if(this.first + this.count > this.total){
            this.total = 0;
            this.count = 0;
            this.first = 0;
        }
        $.post(this.path+"/userBookTag/userBook.do", {
            "page" : page
        }, function(data, status){
            if(status === 'success' && data.code === 200){
                console.log(data);
                SmallBookList.total = data.total;
                SmallBookList.count = data.count;
                SmallBookList.first = data.first;
                SmallBookList.insertIntoInner(data.data);
            }else{
                console.log("获取按用户推荐书籍列表失败");
            }
        });
    }
};